package com.yanqu.road.server.cmd.orewar.task;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.orewar.UserOrePit;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.BaseTask;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.battle.BattleManager;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.List;

public class DetectUserPitDataTask extends BaseTask {

    private long userId;

    private int activityId;

    private int pitId;

    private int patronsCount;

    private BigInteger ability;

    private List<UserPatrons> patronsList;

    public int getActivityId() {
        return activityId;
    }

    public DetectUserPitDataTask(YanQuMessage packet, GamePlayer player) {
        super(packet, player);
    }

    @Override
    protected boolean parseData() throws Exception {
        OreWarProto.OreDetectUserOrePitReqMsg reqMsg = OreWarProto.OreDetectUserOrePitReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        activityId = reqMsg.getActivityId();
        pitId = reqMsg.getPitId();
        return true;
    }

    @Override
    protected void execute() {
        //检测矿坑是否存在
        UserOrePit userOrePit = OreWarMgr.getUserOrePit(activityId, pitId);
        if(null == userOrePit){
            ret = GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND;
            return;
        }
        if(userOrePit.getUserId() != userId){
            ret = GameErrorCode.E_ORE_WAR_ORE_PIT_OWNER_CHANGE;
            return;
        }
        //判断是否足够侦查道具
        if(!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(GameConfig.MINE_WAR_DETECT_CONSUME),
                eLogMoneyType.OreWar, eLogMoneyType.OreWarDetect)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        patronsCount = userOrePit.getPatronsIdList().size();

        patronsList = BattleManager.getCalOreWarEarnSpeed(userOrePit.getPatronsList());

        //获取总实力
//        ability = PatronsMgr.getPatronsListAbility(userId, userOrePit.getObjectIdList());
        ability = PatronsMgr.getOreWarPatronsListEarnSpeed(patronsList);
    }

    @Override
    protected void sendData(GamePlayer player) {
        OreWarProto.OreDetectUserOrePitRespMsg.Builder respMsg = OreWarProto.OreDetectUserOrePitRespMsg.newBuilder();
        if(0 == ret){
            respMsg.setPatronsCount(patronsCount);
            respMsg.setTotalAbility(ability.longValue());
            for (UserPatrons userPatrons : patronsList) {
                PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
                if (null != patronsMsg) {
                    respMsg.addPatronsList(patronsMsg);
                }
            }
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId());
            if(null != userBaseInfo){
                respMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
        }
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_ORE_WAR_DETECT_USER_PIT, respMsg);
    }
}
